package com.heifeng.cloudshelf;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.google.gson.Gson;
import com.heifeng.cloudshelf.constants.HTTPConstants;
import com.heifeng.cloudshelf.dao.PushBean;
import com.heifeng.cloudshelf.event.PushEvent;
import com.heifeng.cloudshelf.mvp.main.activity.MainActivity;
import com.heifeng.cloudshelf.network.NetClient;
import com.heifeng.cloudshelf.push.OnMessageReceiverListener;
import com.heifeng.cloudshelf.utils.ToastUitl;
import com.orhanobut.hawk.Hawk;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.NotificationMessage;

@Deprecated
public class JPushMessageService extends Service implements OnMessageReceiverListener {

    private Gson gson = new Gson();
    private Context _context;

    public JPushMessageService() {
    }

    public static Intent startServer(Context applicationContext) {
        return new Intent(applicationContext, JPushMessageService.class);
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
        _context = getApplicationContext();
//        PushMessageReceiver.setOnMessageReceiverListener(this);
    }

    @Override
    public void onCustomMessage(Context context, CustomMessage customMessage) {

    }

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage message) {

    }

    @Override
    public void onMultiActionClicked(Context context, Intent intent) {

    }

    //    PushEvent
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void PushEvent(Object event) {
    }

    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage message) {
        //接受到的信息
        Log.e("JPushMessageService", "onNotifyMessageArrived:" + message);
        PushBean pushBean = gson.fromJson(message.notificationExtras, PushBean.class);
        Log.e("JPushMessageService", "push:" + pushBean.toString());
        if ("1".equals(pushBean.getType())) {
            NetClient.cliId = "";
            NetClient.token = "";
            HTTPConstants.userId = "";
            HTTPConstants.isLogined = false;
            Hawk.delete(HTTPConstants.USER_ID_KEY);
            Hawk.delete(HTTPConstants.TOKEN_KEY);
            Hawk.delete(HTTPConstants.CLIID_KEY);
            _context.startActivity(new Intent(_context, MainActivity.class));
            ToastUitl.showLong(pushBean.getContent());
        } else if ("2".equals(pushBean.getType()) || "3".equals(pushBean.getType())) {
            Log.e("JPushMessageService", "type:" + pushBean.getType());
            EventBus.getDefault().post(new PushEvent(pushBean));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }

    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage message) {

    }

    @Override
    public void onRegister(Context context, String registrationId) {

    }

    @Override
    public void onConnected(Context context, boolean isConnected) {

    }

    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {

    }
}
